﻿using System;
using System.Data;
using System.Windows.Forms;
using HRM.HRMServiceProxy;
using WeifenLuo.WinFormsUI.Docking;

namespace HRM
{
    public partial class frmInterestRateList : FormBase
    {
        int pageSize = UIParamater.pageSize;
        int currPage = 0;
        int itemCount = 0;
        int totalPage = 0;
        int InterestRateID;
        InterestRateClient wcfInterestRate;
        RoleRightInfo listRole;

        public override void Show_Form()
        {
            this.Show();
        }

        public frmInterestRateList()
        {
            this.KeyPreview = true;
            InitializeComponent();
           // LoadLanguage();
            wcfInterestRate = Service.InterestRate();
            listRole = UIParamater.Quyen_LaiSuatChamNop;
            if (!listRole._create)
                btnNew.Enabled = false;
            if (!listRole._delete)
                btnDel.Enabled = false;
            if (!listRole.Update)
            {
                this.btnEdit.Image = global::HRM.Properties.Resources.app;
                btnEdit.Text = "xem";
            }
            BuildVoucherGrid();
            LoadData();
            
        }
        void LoadLanguage()
        {
            if (this.Tag != null)
            this.Text = this.TabText = UpdateUI.GetString(this.Tag.ToString());
            UpdateUI.Load_ToolStrip(ref toolStrip1);
        }
        private void BuildVoucherGrid()
        {
            UISetting.AddColumn(ref dgvList, "InterestRate_ID", "Mã chính", "", 1, false, true);
            UISetting.AddColumn(ref dgvList, "InterestRate_Code", UpdateUI.GetString("maso"), "", 120, true, true);
            UISetting.AddColumn(ref dgvList, "InterestRate_FromDate", UpdateUI.GetString("tungay"), "", 120, true, true);
            UISetting.AddColumn(ref dgvList, "InterestRate_ToDate", UpdateUI.GetString("denngay"), "", 120, true, true);
            UISetting.AddColumn(ref dgvList, "InterestRate_Percent", UpdateUI.GetString("laisuat"), "", 120, true, true);
            UISetting.AddColumn(ref dgvList, "InterestRate_Description", UpdateUI.GetString("ghichu"), "", 300, true, true);
        }
        private void LoadData()
        {
            DataSet _ds = wcfInterestRate.InterestRate_GetList(out itemCount, currPage, pageSize);
            PageIndex();
            dgvList.Rows.Clear();
            for (int i = 0; i < _ds.Tables[0].Rows.Count; i++)
            {
                dgvList.Rows.Insert(i, 1);
                dgvList.Rows[i].Cells["InterestRate_ID"].Value = _ds.Tables[0].Rows[i]["InterestRate_ID"].ToString();
                dgvList.Rows[i].Cells["InterestRate_Code"].Value = _ds.Tables[0].Rows[i]["InterestRate_Code"].ToString();
                dgvList.Rows[i].Cells["InterestRate_FromDate"].Value = Convert.ToDateTime(_ds.Tables[0].Rows[i]["InterestRate_FromDate"]).ToString("dd/MM/yyyy");
                dgvList.Rows[i].Cells["InterestRate_ToDate"].Value = Convert.ToDateTime(_ds.Tables[0].Rows[i]["InterestRate_ToDate"]).ToString("dd/MM/yyyy");
                dgvList.Rows[i].Cells["InterestRate_Percent"].Value = Convert.ToDecimal(_ds.Tables[0].Rows[i]["InterestRate_Percent"]).ToString();
                dgvList.Rows[i].Cells["InterestRate_Description"].Value = _ds.Tables[0].Rows[i]["InterestRate_Description"].ToString();
            }
        }
        private void PageIndex()
        {
            totalPage = itemCount % pageSize > 0 ? (itemCount / pageSize) + 1 : (itemCount / pageSize);
            if (totalPage == 0)
                lblPageIndex.Text = "0" + "/" + totalPage.ToString();
            else
                lblPageIndex.Text = (currPage + 1).ToString() + "/" + totalPage.ToString();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            Form f = new frmInterestRateInfo(0);
            if (f.ShowDialog(this) == DialogResult.OK)
            {
                currPage = 0;
                LoadData();
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (dgvList.SelectedRows.Count > 0)
            {
                InterestRateID = Convert.ToInt32(dgvList.CurrentRow.Cells["InterestRate_ID"].Value);
                if (wcfInterestRate.InterestRate_Using(InterestRateID) != 0)
                {
                    MessageBox.Show(UpdateUI.GetString("dasudung"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (MessageBox.Show( UpdateUI.GetString("bancochacchanxoa"),  UIParamater.TitleProject, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    if (wcfInterestRate.InterestRate_Delete(InterestRateID))
                    {
                        Program.taskbarNotifier.Show( UIParamater.TitleProject,  UpdateUI.GetString("xoathanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                        LoadData();
                    }
                    else
                    {
                        MessageBox.Show(UpdateUI.GetString("xoakhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (dgvList.SelectedRows.Count > 0)
            {
                InterestRateID = Convert.ToInt32(dgvList.CurrentRow.Cells["InterestRate_ID"].Value);
                Form f = new frmInterestRateInfo(InterestRateID);
                if (f.ShowDialog(this) == DialogResult.OK)
                {
                    LoadData();
                }
            }
        }

        private void btnFisrt_Click(object sender, EventArgs e)
        {
            currPage = 0;
            LoadData();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            totalPage = itemCount % pageSize > 0 ? (itemCount / pageSize) + 1 : (itemCount / pageSize);
            if (totalPage > 0 && currPage != totalPage - 1)
                currPage++;
            LoadData();
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            if (currPage != 0)
                currPage--;
            LoadData();
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            totalPage = itemCount % pageSize > 0 ? (itemCount / pageSize) + 1 : (itemCount / pageSize);
            if (totalPage != 0)
                currPage = totalPage - 1;
            LoadData();
        }

        private void dgvList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
                btnEdit_Click(null, null);
        }

        private void frmInterestRateList_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    this.Dispose();
                    this.Close();
                    break;
                case Keys.F2:
                    btnNew_Click(null, null);
                    break;
                case Keys.F3:
                    btnEdit_Click(null, null);
                    break;
                case Keys.Delete:
                    btnDel_Click(null, null);
                    break;
            }
        }

        private void frmInterestRateList_Activated(object sender, EventArgs e)
        {
            LoadLanguage();
            if(dgvList.Columns.Count>0)
            {
                dgvList.Columns["InterestRate_Code"].HeaderText = UpdateUI.GetString("maso");
                dgvList.Columns["InterestRate_FromDate"].HeaderText = UpdateUI.GetString("tungay");
                dgvList.Columns["InterestRate_ToDate"].HeaderText = UpdateUI.GetString("denngay");
                dgvList.Columns["InterestRate_Percent"].HeaderText = UpdateUI.GetString("laisuat");
                dgvList.Columns["InterestRate_Description"].HeaderText = UpdateUI.GetString("ghichu");
            }
        }

        private void frmInterestRateList_FormClosing(object sender, FormClosingEventArgs e)
        {
            wcfInterestRate.Close();
        }
    }
}
